-- UICampInfo
-- Create by huanghr
-- 阵营信息界面

UICampInfo = class("UICampInfo",function()
    return cc.Layer:create();
end
);

function UICampInfo.create(campId)
    return UICampInfo.new(campId);
end

-- 构造函数
function UICampInfo:ctor(campId)
    -- 初始化
    self:setName("UICampInfo");
    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    local node = cc.CSLoader:createNode("layout/hero/SkillOperate.csb");
    self:addChild(node);
    self.node = node;
    local CT = node:getChildByName("CT");

    self.campId = campId;

    self.CT = CT;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 注册事件处理回调函数
    self:registerEventCallback()

    self:initView();

    -- 适配
    self:resize(node);
end

-- 注册事件处理回调函数
function UICampInfo:registerEventCallback()
    -- 注册英雄佩戴技能的回调
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UICampInfo 界面析构清理");
            EventMgr.removeAll("UICampInfo");
        end
    end);
end

function UICampInfo:initView()
    -- 设置阵营名字
    local nameLabel = findChildByName(self.CT, "name");
    local name = CampM.query(self.campId, "name");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    nameLabel:setString(name);

    local effectLabel = findChildByName(self.CT, "effect");
    effectLabel:setString("");
end

--适配
function UICampInfo:resize(node)
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册点击事件
function UICampInfo:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UICampInfo");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册任意地方点击返回事件
    local btnBgBack = findChildByName(self.node, "bg/bgImg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UICampInfo");
        end
    end
    btnBgBack:addTouchEventListener(onBgBackClick);
end
